﻿Imports DevExpress.XtraSplashScreen
Imports DevExpress.XtraEditors
Public Class XuatHoaDon
    Private m_Ma, m_Ten, m_MaNghiepVu, m_MaPhieu As String
    Private m_Tien As Decimal
    Private m_Flag As Boolean
    Private Sub LoadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.PhieuKhamBenhTableAdapter.FillNgayKham_PhieuKhamBenh(Me.PCV_CLINICDataSet.PhieuKhamBenh, CDate(Me.TuNgayIDateEdit.EditValue), CDate(Me.DenNgayIDateEdit.EditValue))
        Me.DMThoiGianTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMThoiGian)
        Me.BHBanHangTableAdapter.Fill(Me.PCV_CLINICDataSet.BHBanHang, CDate(Me.TuNgayIDateEdit.EditValue), CDate(Me.DenNgayIDateEdit.EditValue), 2)
        Me.BHBanHangChiTietTableAdapter.Fill(Me.PCV_CLINICDataSet.BHBanHangChiTiet, 2, CDate(Me.TuNgayIDateEdit.EditValue), CDate(Me.DenNgayIDateEdit.EditValue))
        Me.DMNghiepVuTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMNghiepVu)
        Me.ThanhToanTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.ThanhToan)
        Me.DoiTuongTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DoiTuong)
        SplashScreenManager.CloseForm()
    End Sub
    Private Sub XuatHoaDon_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.BHBanHangThueTableAdapter.Fill(Me.PCV_CLINICDataSet.BHBanHangThue)
        Me.DMThoiGianISearchLookupEdit.EditValue = "TN"
        LoadDuLieu()
    End Sub
    Public Overrides Sub setSubSau_TaoMoi()
        Me.m_MaPhieu = "XHD" & System.Guid.NewGuid.ToString
        Me.setMacDinhCotAn("ma", m_MaPhieu)
        Me.NgaychungtuIDateEdit.EditValue = Today
        Me.NgayhoadonIDateEdit.EditValue = Today
        Me.HinhthucthanhtoanISearchLookupEdit.EditValue = "TM"
    End Sub
    Private Sub DMThoiGianISearchLookupEdit_EditValueChanged(sender As Object, e As EventArgs) Handles DMThoiGianISearchLookupEdit.EditValueChanged
        setDMThoiGian(Me.DMThoiGianISearchLookupEdit, TuNgayIDateEdit, DenNgayIDateEdit)
        ReLoad()
    End Sub
    Public Sub New()
        Me.IGridViewDsThamChieu1.setColumnCheck()
        Me.IGridViewDsThamChieu2.setColumnCheck()
        InitializeComponent()
        Me.setSoTabThamChieu(2)
        Me.setSoTab(2)
        Me.setObj(Me.PCV_CLINICDataSet, Me.PCV_CLINICDataSet.BHBanHang, BHBanHangBindingSource, TableAdapterManager, BHBanHangTableAdapter, Me.ManghiepvuISearchLookupEdit, Me.HinhthucthanhtoanISearchLookupEdit)
        Me.setTab1(Me.PCV_CLINICDataSet.BHBanHangChiTiet, FKBHBanHangChima3C34F16FBindingSource, BHBanHangChiTietTableAdapter)
    End Sub
    Private Sub ReLoad()
        Dim position As Integer = Me.BHBanHangBindingSource.Position
        LoadDuLieu()
        Me.BHBanHangBindingSource.Position = position
    End Sub
    Protected Overrides Sub BarButtonItemLamTuoi_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs)
        ReLoad()
    End Sub
    Private Sub MakhachhangISearchLookupEdit_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles MakhachhangISearchLookupEdit.EditValueChanging
        Try
            Me.TenkhachhangITextEdit.EditValue = Me.MakhachhangISearchLookupEdit.getRows.Item("ten").ToString()
            Me.DiachiITextEdit.EditValue = Me.MakhachhangISearchLookupEdit.getRows.Item("diachi").ToString
        Catch ex As Exception
        End Try
    End Sub
    Private Sub IGridViewChiTiet01_InitNewRow(sender As Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewChiTiet01.InitNewRow
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("uid", System.Guid.NewGuid)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("mahang", m_Ma)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("tenhang", m_Ten)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("soluonggiao", 1)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("dongiavnd", 0) 'xử lý sau
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("thanhtienvnd", 0) 'xử lý sau
        Me.setFlagInitNewRowTab1(True)
    End Sub
    Private Sub ManghiepvuISearchLookupEdit_EditValueChanged(sender As Object, e As EventArgs) Handles ManghiepvuISearchLookupEdit.EditValueChanged
        If IsDBNull(ManghiepvuISearchLookupEdit.EditValue) = False Then
            Me.m_MaNghiepVu = ManghiepvuISearchLookupEdit.EditValue
            Me.IGridViewDsThamChieu1.setUpdateData()
            'xác định coi có check vô hay chưa ?
            If getIsCheckGridView(Me.IGridViewDsThamChieu1) Then
                If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn ra hóa đơn với các thông tin đã chọn không ?", "Thông tin", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                    For index = 0 To Me.IGridViewDsThamChieu1.RowCount - 1
                        If CBool(Me.IGridViewDsThamChieu1.GetRowCellValue(index, "Mark")) Then
                            'chỉ lấy dòng đầu cho phần thông tin
                            Me.m_Ma = IGridViewDsThamChieu1.GetRowCellValue(index, "mabenhnhan").ToString
                            Me.m_Ten = IGridViewDsThamChieu1.GetRowCellValue(index, "tenbenhnhan").ToString
                            If index = 0 Then
                                Me.MakhachhangISearchLookupEdit.EditValue = IGridViewDsThamChieu1.GetFocusedRowCellValue("mabenhnhan").ToString
                                Dim row As DataRow = getRow(Me.PCV_CLINICDataSet_Don.DoiTuong, DoiTuongBindingSource, "ma", m_Ma)
                                Me.TenkhachhangITextEdit.EditValue = row.Item("ten").ToString
                                Me.DiachiITextEdit.EditValue = row.Item("diachi").ToString
                            End If
                            Me.IGridViewChiTiet01.AddNewRow()
                        End If
                        Me.IGridViewDsThamChieu1.SetRowCellValue(index, "Mark", False)
                    Next
                End If
            End If
        End If
    End Sub
    Private Sub ManghiepvuISearchLookupEdit_EditValueChanging(sender As Object, e As Controls.ChangingEventArgs) Handles ManghiepvuISearchLookupEdit.EditValueChanging
        Me.BHBanHangBindingSource.EndEdit()
    End Sub
    Private Sub ChuyểnDữLiệuThuếToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ChuyểnDữLiệuThuếToolStripMenuItem.Click
        If Me.getState <> 0 Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn chuyển dữ liệu thuế không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.IGridViewChiTiet01.Columns("sohoadon").GroupIndex = 0
                Me.IGridViewChiTiet01.Columns("ngayhoadon").GroupIndex = 1
                For index = 0 To Me.IGridViewChiTiet01.RowCount - 1
                    Me.IGridViewChiTiet01.MoveNext()
                Next
                Me.IGridViewChiTiet01.ClearGrouping()
                Me.TabbedControlGroupChiTiet.SelectedTabPage = Me.TabbedControlGroupChiTiet.TabPages(1) 'chọn tab thue
            End If
        End If
    End Sub
End Class
